﻿using System;
using System.Windows.Forms;

namespace FileSynchronizer.Forms
{
    public partial class Form_Settings : Form
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private bool RunMinimizedSetting;
        private bool RunWhenUserLogsOnSetting;
        private bool SyncAtStartupSetting;
        private bool UseURLforMappedDriveSetting;
        private bool AutoTerminateApplicationSetting;
        private int TerminationWaitTimeSetting;
        private bool DisableFolderWarningSetting;
        private bool EnableEnterpriseFeaturesSetting;
        private bool LatencyEnabledSetting;
        private int LatencyTimeoutSetting;

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public variables and properties
        //---------------------------------------------------------------------------------------------------------------------------------------
        public bool RunMinimized
        {
            get
            {
                return RunMinimizedSetting;
            }
            set
            {
                RunMinimizedSetting = value;
                CheckRunMinimized.Checked = RunMinimizedSetting;
            }
        }

        public bool RunWhenUserLogsOn
        {
            get
            {
                return RunWhenUserLogsOnSetting;
            }
            set
            {
                RunWhenUserLogsOnSetting = value;
                checkAutoStart.Checked = RunWhenUserLogsOnSetting;
            }
        }


        public bool SyncAtStartup
        {
            get
            {
                return SyncAtStartupSetting;
            }
            set
            {
                SyncAtStartupSetting = value;
                CheckAutoSync.Checked = SyncAtStartupSetting;
            }
        }

        public bool UseURLforMappedDrive
        {
            get
            {
                return UseURLforMappedDriveSetting;
            }
            set
            {
                UseURLforMappedDriveSetting = value;
                CheckUseURL.Checked = UseURLforMappedDriveSetting;
            }
        }

        public bool AutoTerminateApplication
        {
            get
            {
                return AutoTerminateApplicationSetting;
            }
            set
            {
                AutoTerminateApplicationSetting = value;
                CheckAutoTerminate.Checked = AutoTerminateApplicationSetting;
                if (AutoTerminateApplicationSetting)
                {
                    TerminationWaitTimeEntry.Enabled = true;
                }
                else
                {
                    TerminationWaitTimeEntry.Enabled = false;
                }
            }
        }

        public bool DisableFolderWarning
        {
            get
            {
                return DisableFolderWarningSetting;
            }
            set
            {
                DisableFolderWarningSetting = value;
                CheckDisableFolderWarning.Checked = DisableFolderWarningSetting;
            }
        }

        public int TerminationWaitTime
        {
            get
            {
                return TerminationWaitTimeSetting;
            }
            set
            {
                TerminationWaitTimeSetting = value;

                TerminationWaitTimeEntry.Value = TerminationWaitTimeSetting;
            }
        }

        public bool EnableEnterpriseFeatures
        {
            get
            {
                return EnableEnterpriseFeaturesSetting;
            }
            set
            {
                EnableEnterpriseFeaturesSetting = value;
                CheckEnableEnterpriseFeatures.Checked = EnableEnterpriseFeaturesSetting;
                if (EnableEnterpriseFeaturesSetting)
                {
                    if (!tabControl1.Contains(Enterprise_Page))
                    {
                        tabControl1.Controls.Add(Enterprise_Page);
                    }
                }
                else
                {
                    if (tabControl1.Contains(Enterprise_Page))
                    {
                        tabControl1.Controls.Remove(Enterprise_Page);
                    }
                }
            }
        }

        public bool LatencyEnabled
        {
            get
            {
                return LatencyEnabledSetting;
            }
            set
            {
                LatencyEnabledSetting = value;
                CheckLatencyEnabled.Checked = LatencyEnabledSetting;
                if (LatencyEnabledSetting)
                {
                    LatencySkipTimeEntry.Enabled = true;
                }
                else
                {
                    LatencySkipTimeEntry.Enabled = false;
                }
            }
        }

        public int LatencyTimeout
        {
            get
            {
                return LatencyTimeoutSetting;
            }
            set
            {
                LatencyTimeoutSetting = value;
                LatencySkipTimeEntry.Value = LatencyTimeoutSetting;
            }
        }
        
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------

        public Form_Settings()
        {
            InitializeComponent();
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Event Handlers
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the User clicks the "OK" Button
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void OnOKClick(object sender, EventArgs e)
        {
            RunMinimizedSetting = CheckRunMinimized.Checked;
            RunWhenUserLogsOn = checkAutoStart.Checked;
            SyncAtStartupSetting = CheckAutoSync.Checked;
            UseURLforMappedDriveSetting = CheckUseURL.Checked;
            AutoTerminateApplicationSetting = CheckAutoTerminate.Checked;
            TerminationWaitTimeSetting = (int)TerminationWaitTimeEntry.Value;
            DisableFolderWarningSetting = CheckDisableFolderWarning.Checked;
            EnableEnterpriseFeaturesSetting = CheckEnableEnterpriseFeatures.Checked;
            LatencyEnabledSetting = CheckLatencyEnabled.Checked;
            LatencyTimeoutSetting = (int)LatencySkipTimeEntry.Value;           
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when the value changes of the TerminationCheckbox
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void OnTerminationCheckChanged(object sender, EventArgs e)
        {
            AutoTerminateApplicationSetting = CheckAutoTerminate.Checked;
            if (AutoTerminateApplicationSetting)
            {
                TerminationWaitTimeEntry.Enabled = true;
            }
            else
            {
                TerminationWaitTimeEntry.Enabled = false;
            }
        }

        private void OnEnterpriseFeaturesChanged(object sender, EventArgs e)
        {
            EnableEnterpriseFeaturesSetting = CheckEnableEnterpriseFeatures.Checked;
            if (EnableEnterpriseFeaturesSetting)
            {
                if (!tabControl1.Contains(Enterprise_Page))
                {
                    tabControl1.Controls.Add(Enterprise_Page);
                }
            }
            else
            {
                if (tabControl1.Contains(Enterprise_Page))
                {
                    tabControl1.Controls.Remove(Enterprise_Page);
                }
            }
        }

        private void OnLatencyEnabledChanged(object sender, EventArgs e)
        {
            LatencyEnabledSetting = CheckLatencyEnabled.Checked;
            if (LatencyEnabledSetting)
            {
                LatencySkipTimeEntry.Enabled = true;
            }
            else
            {
                LatencySkipTimeEntry.Enabled = false;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}
